#include"../headers/server_msg.h"
#include <fcntl.h>
#include <sys/stat.h>
#include <time.h>
#include <string.h>

int init_file(struct server_msg *srvl)
{
	time_t t=time(NULL);
	char buffer[256];

	strftime(buffer, sizeof(buffer), "%X", localtime(&t));
 	srvl->fileName=buffer;
	
	srvl->fd=open(srvl->fileName, O_RDWR|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR);
	if(srvl->fd==-1) 
	{
		printf("Error:: can't create log file\n");
		return -1;
	}
	return 0;
}
int sendMSG(int fileDescriptor, char *msg)
{
	time_t t=time(NULL);
        char buffer[256];

        strftime(buffer, sizeof(buffer), "%X", localtime(&t));
	
	char str[1000];
	strcpy(str, msg);
	strcat(str, "TIME: ");
	strcat(str, buffer);	
	strcat(str, "\n");

	printf("%s\n", str);
	return write(fileDescriptor, str, strlen(str));
}
